home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Information / Programming / Mac Programming FAQ / Mac Programming FAQ 3⁄3 < prev   
Encoding:
Text File  |  1994-12-08  |  18.2 KB  |  465 lines  |  [TEXT/R*ch]

  1. *8* IAC
  2.  
  3. 8.1) Q: What are AppleEvents? 
  4.  
  5. A: AppleEvents are a level-5 network protocol.  If you are not familiar 
  6. with the ISO network stack, this means it's a way of structuring sessions 
  7. between network entities (programs) that is not dependent on the 
  8. underlying protocol (such as PPC or TCP/IP) Despite being a network 
  9. protocol, they can be very useful on Macs that are not on a network.  In 
  10. short, they provide applications with a comprehensive way to send 
  11. arbitrary structured data to other applications (or themselves) which 
  12. receive the events through their main event loop.
  13.  
  14. The AppleEvent Object Model is a way of looking at applications and the 
  15. data they contain, and also a level-6 network protocol.  You _can_ send 
  16. AppleEvent Object Model data through AppleEvents (and the standard 
  17. AppleEvents defined in the AppleEvent Registry use it) but you don't have 
  18. to - unless you want to talk with other applications, of course, then the 
  19. AEOM is a lingua franca.
  20.  
  21. 8.2) Q: What are the four required AppleEvents? 
  22.  
  23. A: There are four events your application really must implement if you 
  24. want to sell it: the kCoreEventClass class, kAEOpenApplication, 
  25. kAEQuitApplication, kAEOpenDocuments and kAEPrintDocuments events IDs.  
  26. When you support these events (or any AppleEvents) you will not get 
  27. startup info through GetAppParams() anymore, unless you run under System 
  28. 6 of course.  The kAEOpenApplication event will be sent to you when the 
  29. user double-clicks your app and it's not started yet.  When receiving it, 
  30. you can put up a new untitled window.
  31.  
  32. kAEOpenDocuments is sent when the user double-clicks your apps documents.  
  33. Note that if the first AppleEvent you receive is a kAEOpenDocuments 
  34. event, the user started your app by double-clicking its documents.
  35.  
  36. kAEPrintDocuments is sent when the user selects your documents and 
  37. chooses "Print" in the Finder menu.  If this is the first AppleEvent you 
  38. receive, you should print the documents and then quit the application 
  39. again; if you received a kAEOpenApplication or kAEOpenDocuments event 
  40. before this, you should just print the documents and close them when 
  41. you're done.
  42.  
  43. kAEQuitApplication is sent to you when the user chooses "Shutdown" or 
  44. "Restart" from the Apple Menu.  You should ask the user whether he wants 
  45. to save any unsaved changed documents, and then quit unless the user 
  46. presses Cancel.
  47.  
  48. Interestingly enough, you can use these four AppleEvents to send even to 
  49. non-AE-aware applications, and the system will translate these events 
  50. into fake menu selections for you.
  51.  
  52. A good way of shutting down the Finder is to send it a Quit AppleEvent.  
  53. You should send a Quit AppleEvent to File Sharing Extension before you 
  54. shut down the Finder, though; the FSE is found by looking for a process 
  55. with the creator 'hhgg'.
  56.  
  57. 8.3) Q: Are there any limits or tradeoffs with AppleEvents? 
  58.  
  59. A: As always, more power means more responsibility. 
  60.  
  61. AppleEvents sent to applications on other Macs require authentification 
  62. the first time they are sent.  If the remote Mac allows Guests to link to 
  63. programs, the INIT AutoGuest 2.0 might help (or the code solution that 
  64. comes with it and you can build into your application)
  65.  
  66. In the first version of the AppleEvent manager, there was a total 64K 
  67. limit on the size of data and overhead.  This limit has been lifted with 
  68. the version of the AppleEvent manager that comes with AppleScript.
  69.  
  70. AppleEvents require a lot of memory copying and handle resizing in their 
  71. construction; this means that large AppleEvents may be slow in 
  72. construction, especially when compared to a pure PPC Toolbox or 
  73. ADSP/ASDSP link.
  74.  
  75. You should use your own application signature as event class for 
  76. AppleEvents you make up, in order not to collide with anybody else.  
  77. Other than that, you are free to make your own events for your own needs, 
  78. though supporting the required events and at least a subset of the Core 
  79. event suite will buy you a lot of functinality from within AppleScript.  
  80. Especially important are the Get Current Selection and Set Current 
  81. Selection events (which are really Get/Set Data on the contents of the 
  82. current selection of the application)
  83.  
  84. The signature for your application SHOULD be registered with DTS to avoid 
  85. conflicts; this is done through e-mail to DEVSUPPORT@AppleLink.Apple.Com 
  86. and the form you use is located on the developer CDs and found on 
  87. ftp.apple.com.
  88.  
  89. 8.4) Q: How do AppleEvents interface with AppleScript? 
  90.  
  91. A: AppleEvents are the meat and potatoes of AppleScript.  If you support 
  92. the AppleEvent Object Model from within your application, users can 
  93. control you through AppleScript.
  94.  
  95. The first thing you should do is get ahold of Inside Mac: 
  96. Interapplication Communication, and a copy of the AppleEvents Registry.  
  97. The former tells you all you ever need to know about AppleEvents, while 
  98. the latter is paramount for implementing the right standard events.  If 
  99. everybody use the standard events, dynamic data interchange between any 
  100. applications will become sweet reality!
  101.  
  102. Then there is the 'aete' resource which lets you put names on the events 
  103. you support, so that users can "Open Terminology" on your application 
  104. from within the Apple Script Editor and use the proper AppleScript 
  105. commands in their scripts.  The format of an aete resource is defined in 
  106. Inside Macintosh: Interapplication Communication.
  107.  
  108. 8.5) Q: Can I compile and run scripts from within my application?
  109.  
  110. A: Yes, this is very simple.  There are toolbox calls for reading 
  111. scripts, compiling scripts, and executing scripts.  (OSACompile, 
  112. OSAExecute) These are all documented in Inside Mac: Interapplication 
  113. Communication.
  114.  
  115. 8.6) Q: Is this a good way of getting a macro language almost 
  116. for free? 
  117.  
  118. A: "Good" is an understatement.  Just let users write scripts, load them 
  119. into menu items and go.  Total systems integration in under a week, 
  120. including adding support for the AEOM to your application.
  121.  
  122. There is source code for an application called "MenuScripter" on the 
  123. developer CD which shows you how to do an application with all of the 
  124. menus being AppleScript scripts.
  125.  
  126. 8.7) Q: Why do I get error -903 (noPortErr) when I try to send an Apple 
  127. Event?
  128.  
  129. A: Make sure the isHighLevelEventAware flag in your application's SIZE 
  130. resource is set.  The AE Manager won't allow you to send events if you're 
  131. not able to receive them.
  132.  
  133. *9* Standalone Code and Linking
  134.  
  135. 9.1) Q: How do I do code resources/extensions/external functions, much 
  136. like HyperCard XCMDs only for my app/extension?
  137.  
  138. A: Here is what you need to do:
  139.  
  140. Define a storage location for the plug-ins.  such as, for an application 
  141. called Foo, a folder called "Foo Pouch/ PlugIns/ Modules/ Tools/ Etc", 
  142. which your installer creates, and which you search for in the same 
  143. folder as your application, the preferences folder, and the System 
  144. Folder (for System 6 (no preferences)) the name of the pouch folder 
  145. should be in a 'STR ' or 'STR#' resource for easy internationalization.
  146.  
  147. Next, define a resource type to hold your plug-in exectuable code.  You 
  148. should allow any resID number, since some development systems have 
  149. restrictions on which id numbers can be used for multi-segment code 
  150. resources.
  151.  
  152. Decide whether you will allow multiple code resources per file.  For 
  153. example, Photoshop lets you bundle an import resource, an export 
  154. resource, and a filter into the same file, each is its own resource 
  155. type.  It uses the resource name to tell the user, so each has its own 
  156. name.
  157.  
  158. Decide on the user interface to plug-ins.  Commonly, at program 
  159. start-up, you scan the pouch and collect all the files of the correct 
  160. types.  Then you scan for appropriate code resources, then put the names 
  161. of the resources in the menus.  You can also use the file names, or run 
  162. the code resources and ask them for the correct name.
  163.  
  164. Decide on the calling convention.  MPW likes to pass parameters as 
  165. longs, and pascal development systems usually can't generate C 
  166. interfaces, so your best bet is to have the entry point of your code 
  167. resource be something like:
  168.  
  169. extern pascal long PlugIn(long selector, CallbackPtr callback,
  170.         long param1, long param2, long param3, long refcon);
  171.  
  172. where params 1 through 3 will have specific meanings depending upon
  173. which function selector is asking to be performed.
  174.  
  175. CallbackPtr is something like this:
  176. typedef struct CallbackRec{
  177.         DrawFunc        erase;
  178.         DrawFunc        paint;
  179. }CallbackRec, *CallbackPtr;
  180.  
  181. where DrawFunc is something like:
  182. typedef pascal void (*DrawFunc)(Rect *);
  183.  
  184. This is the place where you define services that your application will 
  185. perform when asked to by the code resource of the plug-in.
  186.  
  187. You'll need to define a bunch of selectors for what you want the plug-in
  188. to do.
  189.  
  190. You should definately give the plug-in an Init selector, and a dispose 
  191. selector, and have Init return an error or a refcon, which will be 
  192. passed back to the plug-in on each call, so the plug-in can have some 
  193. persistent storage.  You should definately set the CurResFile to the 
  194. plug-in's file while it is running, so it can easily get at any 
  195. resources it needs.
  196.  
  197. The Init selector should pass the interface version in param1 and the 
  198. program version in param2, so that the plug-in can cope with different 
  199. versions of your program.
  200.  
  201. The actual call to the plug-in looks like:
  202.  
  203. *code*
  204.     h = GetResource('PLUG', 1);
  205.     HLock(h);
  206.     xh = StripAddress(h);
  207.     ((PlugInFunc) *xh)(kInit, callbacks, kInterfaceVer1, kProgVer1, 0, 0);
  208.     HUnlock(h);
  209. *end*
  210.  
  211. The StripAddress is important; if your app is running in 24bit 
  212. mode, the resource handle may contain tag bits and you don't 
  213. want strange things to happen if the code resource switches into 
  214. 32bit mode (which QuickDraw may do, incidentally)
  215.  
  216. Exactly how you structure your calling conventions is up to you; 
  217. there is no accepted standard (except for HyperCard XCMDs, but 
  218. that is probably overkill for you). 
  219.  
  220. *10* Compatibility
  221.  
  222. 10.1) Q: I see all these people call Gestalt without first 
  223. checking whether it's implemented. Isn't that bad? 
  224.  
  225. A: No; Gestalt and a few other traps (the HXxx file manager 
  226. traps, and FindFolder) are implemented using glue so they do the 
  227. right thing even if the trap is not implemented. 
  228.  
  229. If you want to get rid of the glue, you can #define 
  230. SystemSevenOrLater (and, using Think C/C++, re-pre-compile 
  231. MacHeaders) However, then you will be responsible for checking 
  232. for these features before you use them. 
  233.  
  234. 10.2) Q: What more functions are implemented in glue? 
  235.  
  236. A: Wake Up and Smell the Glue!
  237.  
  238. How often have you wished you could use that cool new ToolBox 
  239. call, but didn't want to make your application System 7 
  240. dependent? Well, it might be that you *could* in fact have used 
  241. the call. Several traps are implemented in glue, that is, much 
  242. of their functionality is linked into your application and thus 
  243. available even if you are running under an old System. 
  244.  
  245. This list applies to MPW 3.2 and should also be valid for the 
  246. current version of Think C. If you find any inaccuracies, please 
  247. report them to me. (neeri@iis.ee.ethz.ch) 
  248.  
  249. FSOpen: Tries first OpenDF, then Open. 
  250.  
  251. HOpenResFile: Full functionality emulated if trap not available 
  252.  
  253. HCreateResFile: Full functionality emulated if trap not 
  254. available 
  255.  
  256. FindFolder: Under System 6, understands the following values for 
  257. folderType and returns the System Folder for all of them: 
  258.  
  259. kAppleMenuFolderType 
  260.  
  261. kControlPanelFolderType 
  262.  
  263. kExtensionFolderType 
  264.  
  265. kPreferencesFolderType 
  266.  
  267. kPrintMonitorDocsFolderType 
  268.  
  269. kStartupFolderType 
  270.  
  271. kSystemFolderType 
  272.  
  273. kTemporaryFolderType 
  274.  
  275. SysEnvirons: Full functionality emulated if trap not available 
  276.  
  277. NewGestalt: Returns an error if not implemented 
  278.  
  279. ReplaceGestalt: Returns an error if not implemented 
  280.  
  281. Gestalt: The following selectors are always implemented: 
  282.  
  283. vers    mach    sysv    proc    fpu     
  284.  
  285. qd      kbd     atlk    ram     lram    
  286.  
  287. 10.3) Q: I have to support System 6, don't I? 
  288.  
  289. A: It would be foolish to lock yourself out of the many benefits 
  290. the System 7 API provides for software that you start to write 
  291. now. Some of the System 6 and older things (likely SFGetFile and 
  292. wdRefNums among others) will be phased out of the interfaces and 
  293. lose support; especially on future platforms. 
  294.  
  295. The installed base of System 7 is larger than that of System 6; 
  296. this is not surprising because Apple has been shipping System 7 
  297. for several years with all new machines, including the LCII, 
  298. Classic II, Performas and Color Classic. Another argument is 
  299. that newer computer owners (having System 7) are much more 
  300. likely to buy new software than old computer owners who have 
  301. systems that already do what they want them to. 
  302.  
  303. The added work to support both System 6 and System 7 is 
  304. significant; if you have the time and money you may want to do 
  305. it, but only supporting System 6 and not System 7 is doomed to 
  306. fail in the market of today. 
  307.  
  308. Some may call this position subjective; I call it business sense 
  309. based on market demographics. A rule of thumb may be that if you 
  310. target color machines only, you can just as well demand System 7 
  311. as well. 
  312.  
  313. 10.4) Q: Why does my application work on an SE with accellerator 
  314. (or a Mac II or Quadra), but not on one without? 
  315.  
  316. A: Assuming you're not calling Color QuickDraw (which is not 
  317. available on accellerated SEs), you most probably have an 
  318. odd-aligned word access somewhere. 
  319.  
  320. The 68000 does not allow words or longwords to be read from odd 
  321. addresses, while the 68020 and up relaxes this restriction (it 
  322. still is slower than aligned-word access though) 
  323.  
  324. This may or may not crash depending on your compiler: 
  325.  
  326. *code*
  327. struct foo {
  328.    char c1 ;
  329.    char c2 ;
  330.    char c3 ;
  331.    char c4 ;
  332.    char c5 ;
  333. } bar ;
  334.  
  335.    long * x = ( long * ) & bar . c2 ;
  336.    * x = 0x12345678 ; /* X is odd if compiler doesn't pad */
  337.  
  338. This WILL crash on an SE/Plus/Classic/PB100:
  339.  
  340. char foo [ 10 ] ;
  341.  
  342.    long * x = ( long * ) & foo [ 1 ] ;
  343.    * x = 0x12345678 ;
  344.  
  345. *end*
  346.  
  347. 10.5) Q: Why does my application work on a IIci but not on a 
  348. Quadra? 
  349.  
  350. A: Two reasons: 
  351.  
  352. 1) The Quadras 900 and 950 have special processors that handle the serial 
  353. ports; if you write directly to the serial chips, you will crash (this 
  354. goes for the IIfx as well)
  355.  
  356. 2) The Quadras have 68040 processors, as have the Centrises.  These 
  357. processors have separate instruction and data caches (like the 68030) but 
  358. they are larger (4K each) and unlike the 68030 which is write-through 
  359. data cached, the 68040 is copy-back data cached.  This means that changes 
  360. you make to "your code" aren't really changed all the time, since the 
  361. changes may still be in the data cache and not written to memory when the 
  362. CPU reads that part of memory into its I-cache.  Even worse; that part 
  363. might already have been read into the I-cache before you change it in the 
  364. D-cache, meaning that writing out the D-cache will still not be enough.  
  365. You need to flush both the caches when writing self-modifying code.
  366.  
  367. Self-modifying code includes code that builds its own jump tables and 
  368. code that decrypts itself and code that "stubs" MDEFs or WDEFs to jump 
  369. back into the application code.
  370.  
  371. You flush the cache using FlushDataCache() which is implemented if 
  372. Gestalt says you have a 68020 or better processor (or if the _HwDispatch 
  373. trap is implemented)
  374.  
  375. 10.6) Q: Why does my application work on my Quadra but not on my 
  376. accellerated SE?
  377.  
  378. A: You're probably calling Color QuickDraw without first checking if it's 
  379. available.  The following machines do not have color QuickDraw in ROM nor 
  380. RAM:
  381.  
  382. Mac Plus, Mac SE, Mac Classic, Mac Luggable, PowerBook 100, Outbound
  383.  
  384. 10.7) Q: I do check for color quickdraw, but crash nevertheless. 
  385.  
  386. A: _Gestalt lies under some versions of System 7; it says that non-color 
  387. machines HAVE color QuickDraw when you test using the 
  388. gestaltQuickdrawFeatures selector.
  389.  
  390. Instead, check the gestaltQuickdrawVersion selector, if it returns >= 
  391. gestalt8BitQuickdraw then you can safely use gestaltQuickdrawFeatures, 
  392. else you only have b/w QuickDraw.
  393.  
  394. 10.8) Q: Why are there no C/C++ compilers that optimize for the Mc68040?
  395.  
  396. A: Beats me; optimizing for the 68040 can make programs up to 50% faster 
  397. on that chip while still losing nothing, or very little (less than 10%) 
  398. on older chips.
  399.  
  400. *11* Optional System Software
  401.  *11.1* QuickTime
  402.  
  403. 11.1.1) Q: I want to write a Amiga QuickTime player and need the 
  404. CODEC format details. 
  405.  
  406. A: Although the structure of QuickTime movies is well documented in 
  407. Inside Mac: QuickTime, the inner workings of the Apple compression 
  408. modules is a trade secret that Apple will only license to you at great 
  409. cost.  Perhaps it's time for a freeware, cross-platform QuickTime codec?
  410.  
  411. *12* Third-Party Solutions
  412.  
  413. 12.1) Q: What are some royalty-free databases available for the 
  414. Macintosh?
  415.  
  416. These are commercial with no run-time licenses:
  417.  
  418. Prograph CPX comes with a database that can do indexes and tables.  
  419. <sales@prograph.ca>
  420.  
  421. C-Index Pro is an engine that comes with source, does tables and indexes 
  422. in one compound file.  <triosystems@applelink.apple.com>
  423.  
  424. dtF is a relational database library.  <DTF.AMERICA@applelink.apple.com>
  425.  
  426. NeoAccess is an object-oriented database, where you can store objects and 
  427. retrieve by field value; it handles inheritance as well.  <comes as a 
  428. demo on the CodeWarrior CD with an unlockable archive of the real thing>
  429.  
  430. *13* Dessert
  431.  
  432. 13.1) Q: Dessert? 
  433.  
  434. A: Honey Hill Farms Cookie Jar Frozen Yoghurt or Haagen-Dazs Raspberry & 
  435. Cream Ice Cream.
  436.  
  437. Hokey-Pokey icecream with chocolate sauce and (for those who like their 
  438. brain food firmer) Almond and Double Chocolate CookieTime cookies!?
  439.  
  440. *14* Contributors
  441.  
  442. This is obviously an incomplete list as far as it reflects pre-10/31/94 
  443. submissions, but I will credit new additions to the best of my ability.
  444.  
  445. Denis Birnie
  446. Scott Bronson <urge@mcl.mcl.ucsb.edu>
  447. Tim Converse <converse@cs.uchicago.edu>
  448. Bruce Hoult <Bruce@hoult.actrix.gen.nz>
  449. Matthias Neeracher
  450. David P. Oster <oster@netcom.com>
  451. Eric Slosser <slosser@apple.com>
  452. Chris Thomas <thunderone@delphi.com>
  453. Jon W..atte <h+@nada.kth.se>
  454.  
  455. --------------------comp.sys.mac.programmer.info---------------------
  456. comp.sys.mac.programmer.info  is  primarily  for  distributing  FAQs,
  457. tutorials, news, and  similar  documents  related to programming  the
  458. Macintosh.  To post, email csmp_info@xplain.com
  459. -----------------------about MacTech Magazine----------------------
  460. PO Box 250055, Los Angeles, CA 90025, 310-575-4343, Fax:310-575-0925
  461. For more info, anonymous ftp to ftp.netcom.com and cd to /pub/xplain
  462. or email to the following @xplain.com : custservice, editorial, 
  463. adsales, marketing, accounting, pressreleases, progchallenge, 
  464. publisher, info
  465.